{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "# @Author  : Feng Cheng\n",
    "# @Email   : fengcheng@pku.edu.cn\n",
    "\n",
    "# -*- coding: utf-8 -*-\n",
    "from selenium import webdriver\n",
    "import codecs\n",
    "import time\n",
    "import winsound\n",
    "\n",
    "import re\n",
    "#from lxml import html\n",
    "import pdb\n",
    "def getPrice(strPrice):\n",
    "    import pdb\n",
    "    print(type(strPrice))\n",
    "    strData = ''.join(filter(lambda xx: xx.isdigit(), strPrice.encode('utf-8')))\n",
    "    print(strData)\n",
    "    return int(strData)*1.0/100\n",
    "\n",
    "def crawler():\n",
    "    file_output = codecs.open('thefile.csv', 'w', 'utf_8_sig')\n",
    "    print(\"正在抓取 抱财网\")\n",
    "    url = 'https://www.baocai.com/invest/change/index.html'\n",
    "    i = 0\n",
    "    browser = webdriver.Chrome()\n",
    "    pdb.set_trace()\n",
    "    # browser = webdriver.firefox()\n",
    "    browser.implicitly_wait(1)\n",
    "    browser.set_page_load_timeout(1)\n",
    "    browser.set_script_timeout(1)\n",
    "    \n",
    "    exist = {}\n",
    "    try:\n",
    "        browser.get(url)\n",
    "    except:\n",
    "        print('timeout')\n",
    "    k = 1\n",
    "    while 1:\n",
    "        print(\"刷新第\"+str(k)+\"遍\")\n",
    "\n",
    "        time.sleep(1)\n",
    "        price = browser.find_elements_by_xpath('//div[@class=\"list_detail\"]/ul/li/div/span[@style=\"color:#ff8142;\"]')\n",
    "        urls = browser.find_elements_by_xpath('//div[@class=\"item_right\"]/div[@class=\"list_bottom\"]/div/a')\n",
    "        for x in range(8):\n",
    "            priceNow = getPrice(price[x*2].text)\n",
    "            Revneue = getPrice(price[x * 2+1].text)\n",
    "            url = urls[x].get_attribute('href')\n",
    "            discont = priceNow/(priceNow+Revneue)*100\n",
    "            if url in exist:\n",
    "                print(\"该标的已存在\")\n",
    "                continue\n",
    "            else:\n",
    "                exist[url] = 1\n",
    "            print(url)\n",
    "            print('Now Price:', priceNow ,'\\t Revenue:',Revneue,'\\t  Rate of return:',Revneue/priceNow*100, '%\\trate discount:',discont ,'%')\n",
    "            if discont < 0.2:\n",
    "                winsound.Beep(1000, 1000)\n",
    "        try:\n",
    "            browser.refresh()\n",
    "        except:\n",
    "            print('timeout')\n",
    "        time.sleep(3)\n",
    "        k += 1\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在抓取 抱财网\n",
      "timeout\n",
      "刷新第1遍\n"
     ]
    },
    {
     "ename": "TimeoutException",
     "evalue": "Message: timeout\n  (Session info: chrome=68.0.3440.84)\n  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTimeoutException\u001b[0m                          Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-710e07ca8f27>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     20\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     21\u001b[0m     \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 22\u001b[1;33m     \u001b[0mprice\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbrowser\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_elements_by_xpath\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'//div[@class=\"list_detail\"]/ul/li/div/span[@style=\"color:#ff8142;\"]'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     23\u001b[0m     \u001b[0murls\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbrowser\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_elements_by_xpath\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'//div[@class=\"item_right\"]/div[@class=\"list_bottom\"]/div/a'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     24\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mfind_elements_by_xpath\u001b[1;34m(self, xpath)\u001b[0m\n\u001b[0;32m    407\u001b[0m             \u001b[0melements\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_elements_by_xpath\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"//div[contains(@class, 'foo')]\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    408\u001b[0m         \"\"\"\n\u001b[1;32m--> 409\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfind_elements\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mby\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mBy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mXPATH\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mxpath\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    410\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    411\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mfind_element_by_link_text\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlink_text\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mfind_elements\u001b[1;34m(self, by, value)\u001b[0m\n\u001b[0;32m    993\u001b[0m         return self.execute(Command.FIND_ELEMENTS, {\n\u001b[0;32m    994\u001b[0m             \u001b[1;34m'using'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mby\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 995\u001b[1;33m             'value': value})['value'] or []\n\u001b[0m\u001b[0;32m    996\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    997\u001b[0m     \u001b[1;33m@\u001b[0m\u001b[0mproperty\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self, driver_command, params)\u001b[0m\n\u001b[0;32m    318\u001b[0m         \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcommand_executor\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver_command\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    319\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 320\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merror_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcheck_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    321\u001b[0m             response['value'] = self._unwrap_value(\n\u001b[0;32m    322\u001b[0m                 response.get('value', None))\n",
      "\u001b[1;32m~\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py\u001b[0m in \u001b[0;36mcheck_response\u001b[1;34m(self, response)\u001b[0m\n\u001b[0;32m    240\u001b[0m                 \u001b[0malert_text\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'alert'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'text'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    241\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0malert_text\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 242\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    243\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    244\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_value_or_default\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdefault\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTimeoutException\u001b[0m: Message: timeout\n  (Session info: chrome=68.0.3440.84)\n  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)\n"
     ]
    }
   ],
   "source": [
    "file_output = codecs.open('thefile.csv', 'w', 'utf_8_sig')\n",
    "print(\"正在抓取 抱财网\")\n",
    "url = 'https://www.baocai.com/invest/change/index.html'\n",
    "i = 0\n",
    "# browser = webdriver.Chrome()\n",
    "browser = webdriver.Chrome()\n",
    "# browser = webdriver.firefox()\n",
    "browser.implicitly_wait(10)\n",
    "browser.set_page_load_timeout(10)\n",
    "browser.set_script_timeout(10)\n",
    "\n",
    "exist = {}\n",
    "try:\n",
    "    browser.get(url)\n",
    "except:\n",
    "    print('timeout')\n",
    "k = 1\n",
    "while 1:\n",
    "    print(\"刷新第\"+str(k)+\"遍\")\n",
    "\n",
    "    time.sleep(1)\n",
    "    price = browser.find_elements_by_xpath('//div[@class=\"list_detail\"]/ul/li/div/span[@style=\"color:#ff8142;\"]')\n",
    "    urls = browser.find_elements_by_xpath('//div[@class=\"item_right\"]/div[@class=\"list_bottom\"]/div/a')\n",
    "    for x in range(8):\n",
    "        priceNow = getPrice(price[x*2].text)\n",
    "        Revneue = getPrice(price[x * 2+1].text)\n",
    "        url = urls[x].get_attribute('href')\n",
    "        discont = priceNow/(priceNow+Revneue)*100\n",
    "        if url in exist:\n",
    "            print(\"该标的已存在\")\n",
    "            continue\n",
    "        else:\n",
    "            exist[url] = 1\n",
    "        print(url)\n",
    "        print('Now Price:', priceNow ,'\\t Revenue:',Revneue,'\\t  Rate of return:',Revneue/priceNow*100, '%\\trate discount:',discont ,'%')\n",
    "        if discont < 0.2:\n",
    "            winsound.Beep(1000, 1000)\n",
    "    try:\n",
    "        browser.refresh()\n",
    "    except:\n",
    "        print('timeout')\n",
    "    time.sleep(3)\n",
    "    k += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "''.join(filter(lambda x: x.isdigit(), '123,321'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
